/*
 * @Author: your name
 * @Date: 2021-12-09 19:40:33
 * @LastEditTime: 2021-12-09 20:55:18
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \12.9work\pages\index.js
 */
const koa=require('koa')
const Router=require('koa-router')
const Mock=require('mockjs')
const bodyParser=require('koa-bodyparser')
const app=new koa()
const router=new Router()
app.use(bodyParser())

const {list}=Mock.mock({
    "list|100":[{
        "id":"@id",
        "name":"@cname",
        "title":"@ctitle",
        "city":"@city(true)"
    }]
})
router.get('/api/getList',async(ctx,next)=>{
    let {pageSize,pageIndex}=ctx.query
    let newList=list.slice((pageIndex-1)*pageSize,pageIndex*pageSize)
    ctx.body={
        newList,
        total:list.length
    }
}) 
router.post('/api/edit',async(ctx,next)=>{
    console.log(ctx.request.body)
    let {name,title,city}=ctx.request.body;
    list.unshift({
        id:new Date().toLocaleDateString().toString(),
        name,
        title,
        city
    })
    ctx.body='添加成功'
    console.log(list[0])
})
app.use(router.routes())
app.listen(3000,()=>{
    console.log('Service started successfully in 3000!!!! ')
})